﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using NAnt.Core;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace NAntMenu.Logger
{
	public class LevelToImageConverter : IValueConverter
	{
		private static BitmapImage _errorImage = null;
		private static BitmapImage _warningImage = null;
		private static BitmapImage _infoImage = null;

		private const string AssemblyName = "NAntMenu.Logger";

		#region IValueConverter Members

		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			Image image = null;

			//validate input
			if (!(value is Level))
			{
				return image;
			}

			Level level = (Level)value;
			switch (level)
			{
				case Level.Error:
					if (_errorImage == null)
					{
						_errorImage = new BitmapImage(new Uri(string.Format("/{0};component/images/Error.png", AssemblyName),
						UriKind.RelativeOrAbsolute));
					}

					image = new Image();
					image.Source = _errorImage;
					break;

				case Level.Warning:
					if (_warningImage == null)
					{
						_warningImage = new BitmapImage(new Uri(string.Format("/{0};component/images/Warning.png", AssemblyName),
						UriKind.RelativeOrAbsolute));
					}

					image = new Image();
					image.Source = _warningImage;
					break;
				case Level.Info:
					if (_infoImage == null)
					{
						_infoImage = new BitmapImage(new Uri(string.Format("/{0};component/images/Info.png", AssemblyName),
						UriKind.RelativeOrAbsolute));
					}

					image = new Image();
					image.Source = _infoImage;
					break;
			}

			if (image != null)
			{
				image.MaxWidth = 16;
				image.MaxHeight = 16;
			}

			return image;
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}

		#endregion
	}
}
